
import sys
#import cv2
#import numpy as np
import logging
from common.utils import *
import os
import json
import shutil
from defconfig import *

config['progname'] = progname

def get_max_disk():
    import shutil
    disks = 'CDEFG'
    tt = []
    for d in disks:
        try:
            total, used, free = shutil.disk_usage(f'{d}:')
            #print(total, used, free)
            tt.append(total)
        except Exception as e:
            break
    i = np.argmax(tt)
    return disks[i]

def get_out_img_p0():
    D = get_max_disk()
    out_img_p = f'{D}:/data/save/{progname}'
    return out_img_p

out_img_p = get_out_img_p0()
mkdir(out_img_p)

def get_out_img_p():
    d = get_date_stamp()
    outp = f'{out_img_p}/{d}'
    mkdir(outp)
    return outp

def get_outp(index, end):
    d = get_date_stamp()
    s = get_time_stamp()
    aa = 'NG' if end else 'OK'
    outp = f'{out_img_p}/{d}/{aa}/{s}'
    mkdir(outp)
    return outp

config_fn = f'{out_img_p}/config.json'

for x in thd_conf:
    config[x] = thd_conf[x][0]

for x in chk_conf:
    config[x] = chk_conf[x][0]


def config_load():
    try:
        d = json.load(open(config_fn, 'r'))
        return d
    except Exception as e:
        pass

    return {}

def config_save():
    json.dump(config, open(config_fn, 'w'))

if not os.path.exists(config_fn):
    config_save()

config.update(config_load())
print(config)
try:
    d = get_date_stamp()
    mkdir(f'{out_img_p}/{d}')
    aress =f'{out_img_p}/{d}/tongji.json'
    with open(aress,'r',encoding='UTF-8') as f:
        tongji = json.load(f)
        f.close()
except Exception as e:
    tongji = {
        'cnt_ng': 0,
        'cnt_ok': 0,
        'cnt_all': 0,
    }

def update_tongji(end):
    tongji['cnt_all'] += 1
    if end:
        tongji['cnt_ng'] += 1
    else:
        tongji['cnt_ok'] += 1
    # d = get_date_stamp()
    # aress = f'{out_img_p}/{d}/tongji.json'
    with open(aress, 'w', encoding='UTF-8') as f:
        json.dump(tongji,f)
        f.close()
    return 0


if __name__ == "__main__":
    pa = 'D:/data/220329竹筷/pic'
    pa = 'D:/data/220329竹筷'
    # del_last_img()
    # cv2.destroyAllWindows()
